<?php
/**
 * Import CT_RaidTracker Ignore Items
 *
 * @category Plugins
 * @package CT_RaidTrackerImport
 * @copyright (c) 2006, EQdkp <http://www.edqkp.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * @author Garrett Hunter <loganfive@blacktower.com>
 * $Rev: 84 $ $Date: 2007-04-22 09:42:16 +0000 (Sun, 22 Apr 2007) $
 */

if ( !defined('EQDKP_INC') )
{
    die('Hacking attempt');
}
include_once("admin/ctrt_xml.php");

/**
 * This class imports ignore items
 * @subpackage ManageCTRT
 */
class CTRT_ImportIgnoreItems extends CTRT_ManageIgnoreItems {

    var $_myMode = "import";
	var $_sampleXML = "
&lt;IgnoreItems&gt;<br />
&nbsp;&nbsp;&lt;IgnoreItem&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"positive\">18562</span><br />
&nbsp;&nbsp;&lt;/IgnoreItem&gt;<br />
&nbsp;&nbsp;&lt;IgnoreItem&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"positive\">20725</span><br />
&nbsp;&nbsp;&lt;/IgnoreItem&gt;<br />
&lt;/IgnoreItems&gt;";

    function CTRT_ImportIgnoreItems()
    {
        global $db, $eqdkp, $user, $tpl, $pm;
        global $SID;

        parent::eqdkp_admin();

        $this->daoIgnoreItem = new CTRT_IgnoreItem();
        $this->parser = new CTRT_XML();

        $this->assoc_buttons(array(
            'import' => array(
                'name'    => 'import',
                'process' => 'process_import',
                'check'   => 'a_raid_add'),
            'form' => array(
                'name'    => '',
                'process' => 'display_form',
                'check'   => 'a_raid_add'))
        );
    }

    /**
     * Display form
     */
    function display_form()
    {
        global $db, $eqdkp, $user, $tpl, $pm;
        global $SID;

        $this->createMenus();

        $tpl->assign_vars(array(
            // Form vars
            'XML'               => $_POST['xml'],
			'SAMPLE_XML'		=> $this->_sampleXML,

            // Language
            'L_PLUGIN_TITLE'    => $user->lang['ctrt'],

            // Buttons
            'L_IMPORT'    		=> $user->lang['ctrt_import'],

            // Help text
            'L_HELP'            => $user->lang['ctrt_help_import'],
            'L_HELP_FORMAT'     => $user->lang["ctrt_help_import_format"],

            // Form validation
            'FV_INVALID_XML'        => $this->fv->generate_error('xmlError'),

        ));

        $eqdkp->set_vars(array(
            'page_title'    => sprintf($user->lang['admin_title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['ctrt_adminmenu_title'].": ".$user->lang['ctrt_adminmenu_'.$this->getMyParam()."_".$this->getMyMode()],
            'template_path' => $pm->get_data('ctrt', 'template_path'),
            'template_file' => 'admin/import.html',
            'display'       => true)
        );
    }

    /**
     * Validate the xml string
     */
    function error_check()
    {
        global $user;

        $xml = $this->parser->validateXML(stripslashes($_POST['xml']));
        if (!$xml) {
            $this->fv->is_filled('xmlError', $user->lang['ctrt_fv_invalid_xml']);
        }
        return $this->fv->is_error();
    }

    /**
     * Import the aliases
     */
    function process_import () {
        global $db, $eqdkp, $user, $tpl, $pm;
        global $SID;

        $message = "";

        $ignoreItems = $this->parser->xml_import(stripslashes($_POST['xml']));
		
        foreach ($ignoreItems["ignoreitems"] as $ignoreitem) {
			
			if (!is_array($ignoreitem)) {
				$ignoreitem = array($ignoreitem);
			}
			foreach ($ignoreitem as $item) {
				if (!$this->daoIgnoreItem->insert(array(
												  'ignore_items_wow_id'=>$item
												  ))) {
					/**
					 * This trigger exists
					 */
					$message .= sprintf($user->lang["ctrt_item_duplicate"]."<br />",
										$this->daoIgnoreItem->getIgnoreItemWoWId()
										);
				} else {
				/* @TODO
				   IMPORT SUCCESS - LOG RECORD HERE */
					$message .= sprintf($user->lang["ctrt_item_success_add"]."<br />",
										$this->daoIgnoreItem->getIgnoreItemWoWId()
										);
				}
			}
		}

        $link_list = array(
            $user->lang['ctrt_adminmenu_'.$this->getMyParam()."_add"]  => $this->getControllerLink($this->getMyParam(),"add"),
            $user->lang['ctrt_adminmenu_'.$this->getMyParam()."_list"] => $this->getControllerLink($this->getMyParam(),"list"));

        //
        // Success message
        //
        $this->admin_die($message, $link_list);
    }

}
?>
